package com.example.demo.demos.gui;

import com.example.demo.demos.service.UploadListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import javax.annotation.PostConstruct;

@Component
public class MainFrame extends JFrame {
    private JPanel mainPanel;
    private JButton page1Btn, page2Btn, page3Btn;
    private JPanel buttonPanel;

    @Autowired
    private Page1Panel page1;

    @Autowired
    private Page2Panel page2;

    @Autowired
    private Page3Panel page3;

    // 定义每个页面对应的选中背景色
    private static final Color PAGE1_SELECTED_COLOR = new Color(220, 240, 255); // 浅蓝色
    private static final Color PAGE2_SELECTED_COLOR = new Color(220, 255, 230); // 浅绿色
    private static final Color PAGE3_SELECTED_COLOR = new Color(255, 240, 220); // 浅橙色

    public MainFrame() {
        setTitle("文件选择系统");
        setSize(980, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    @PostConstruct
    public void initUI() {
        mainPanel = new JPanel(new CardLayout());
        buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
        buttonPanel.setBackground(Color.WHITE);

        // 初始化按钮并设置各自的选中样式
        initButtons();

        buttonPanel.add(page1Btn);
        buttonPanel.add(page2Btn);
        buttonPanel.add(page3Btn);

        page1.loadEmailHistory();

        // 添加事件监听
        setupButtonListeners();

        // 添加组件到窗口
        add(buttonPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);

        // 添加页面到卡片布局
        mainPanel.add(page3, "page3");
        mainPanel.add(page2, "page2");
        mainPanel.add(page1, "page1");

        // 初始显示第一个页面
        showPanel("page3");
        setButtonSelected(page3Btn, true);
        page3.loadPage(0);
        setVisible(true);
    }

    private void initButtons() {


        // 页面2按钮 - 邮件发送列表
        page2Btn = createPageButton("邮件发送列表");
        // 设置页面2按钮的选中背景
        page2Btn.putClientProperty("JButton.selectedBackground", PAGE2_SELECTED_COLOR);

        // 页面3按钮 - 文件上传列表
        page3Btn = createPageButton("文件上传列表");
        // 设置页面3按钮的选中背景
        page3Btn.putClientProperty("JButton.selectedBackground", PAGE3_SELECTED_COLOR);
        // 页面1按钮 - 文件上传页面
        page1Btn = createPageButton("文件上传页面");
        // 设置页面1按钮的选中背景
        page1Btn.putClientProperty("JButton.selectedBackground", PAGE1_SELECTED_COLOR);
    }

    /**
     * 创建统一样式的页面切换按钮
     */
    private JButton createPageButton(String text) {
        JButton button = new JButton(text);
        // 设置按钮基本样式
        button.setPreferredSize(new Dimension(140, 35));
        button.putClientProperty("JButton.arc", 5);
        button.putClientProperty("JButton.buttonType", "toolBarButton");
        button.setFocusPainted(false);
        return button;
    }

    /**
     * 设置按钮选中状态并更新样式
     */
    private void setButtonSelected(JButton button, boolean selected) {
        button.setSelected(selected);
        // 手动触发UI更新
        button.repaint();
    }

    private void setupButtonListeners() {
        page1Btn.addActionListener(e -> {
            showPanel("page1");
            updateButtonStates(page1Btn);
            page1.loadEmailHistory();
        });

        page2Btn.addActionListener(e -> {
            showPanel("page2");
            updateButtonStates(page2Btn);
            page2.loadPage(0);
        });

        page3Btn.addActionListener(e -> {
            showPanel("page3");
            updateButtonStates(page3Btn);
            page3.loadPage(0);
        });
    }

    /**
     * 更新所有按钮的选中状态
     */
    private void updateButtonStates(JButton selectedButton) {
        setButtonSelected(page1Btn, selectedButton == page1Btn);
        setButtonSelected(page2Btn, selectedButton == page2Btn);
        setButtonSelected(page3Btn, selectedButton == page3Btn);
    }

    private void showPanel(String panelName) {
        CardLayout cl = (CardLayout) mainPanel.getLayout();
        cl.show(mainPanel, panelName);
    }
}
